﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace DevicesView.Logic
{
    public class ImageTool
    {
        public static Image LoadImage(string path)
        {
            byte[] buffer = LoadFile(path);
            if (buffer != null)
                using (MemoryStream ms = new MemoryStream(buffer))
                    return Image.FromStream(ms);
            return null;
        }

        public static byte[] LoadFile(string path)
        {
            if (string.IsNullOrEmpty(path))
                return null;

            if (!path.StartsWith("http") && File.Exists(path))
            {
                using (FileStream fs = File.OpenRead(path))
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, (int)fs.Length);
                    return buffer;
                }
            }

            HttpItem item = new HttpItem { Url = path, Method = HttpMethod.Get };
            HttpResult<byte[]> result = WebLogic.GetHttpResult<byte[]>(item);
            if (result.Success && result.Result != null && result.Result.Length > 0)
                return result.Result;
            return null;
        }
    }
}
